package com.pactera.asmp.server.restfullapi;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.pactera.asmp.server.common.annotations.JwtIgnore;
import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.entity.ProductionLine;
import com.pactera.asmp.server.entity.ProductionLineSearchDTO;
import com.pactera.asmp.server.entity.ProjectProductionConfig;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.pojo.ProductionLineBuildNoticeInfo;
import com.pactera.asmp.server.pojo.ProductionLineInfo;
import com.pactera.asmp.server.service.IProductionLineService;

@RestController
@RequestMapping("/api/v1")
public class RestfulProductionLineApi {

    private static final Logger logger = LoggerFactory.getLogger(RestfulProductionLineApi.class);

    @Autowired
    private IProductionLineService productionLineService;

    @GetMapping(value = "/productionLine/listByProjectId")
    public ResponseResult<List<ProductionLine>> getProductionLineListByProjectId(@RequestParam("projectId") Integer projectId) {
        logger.info("GET /api/v1/productionLine/listByProjectId getProductionLineListByProjectId.{}", projectId);
        return ResponseResult.defaultSuccess(productionLineService.getProductionLineListByProjectId(projectId));
    }

    @PostMapping(value = "/productionLine/list", produces = "application/json;charset=UTF-8")
    public ResponseResult<List<ProductionLineInfo>> getProductionLineList(@RequestBody ProductionLineSearchDTO productionLineSearchDTO) {
        logger.info("POST /api/v1/productionLine/list getProductionLineList.{}", JSONObject.toJSONString(productionLineSearchDTO));
        return ResponseResult.defaultSuccess(productionLineService.getProductionLineList(productionLineSearchDTO));
    }

    @PostMapping(value = "/productionLine/batch", produces = "application/json;charset=UTF-8")
    public ResponseResult batchProductionLine(@RequestBody @Valid ProductionLineInfo productionLineInfo) {
        logger.info("POST /api/v1/productionLine/batch batchProductionLine.{}", JSONObject.toJSONString(productionLineInfo));
        if(CollectionUtils.isEmpty(productionLineInfo.getProductionLineVos())) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        return productionLineService.batchProductionLine(productionLineInfo);
    }

    @PutMapping(value = "/productionLine", produces = "application/json;charset=UTF-8")
    public ResponseResult updateProductionLine(@RequestBody ProductionLine productionLine) {
        logger.info("PUT /api/v1/productionLine updateProductionLine.{}", JSONObject.toJSONString(productionLine));
        if(StringUtils.isEmpty(productionLine.getJobName())
                || StringUtils.isEmpty(productionLine.getJobUrl())
                || null == productionLine.getId()
                || null == productionLine.getProjectId()
                || null == productionLine.getProjectModuleId()) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        return productionLineService.updateProductionLine(productionLine);
    }

    @DeleteMapping(value = "/productionLine/{id}", produces = "application/json;charset=UTF-8")
    public ResponseResult deleteProductionLine(@PathVariable("id") Integer id) {
        logger.info("PUT /api/v1/productionLine/{} deleteProductionLine.", id);
        return productionLineService.deleteProductionLine(id);
    }

    @PutMapping(value = "/productionLine/config", produces = "application/json;charset=UTF-8")
    public ResponseResult updateProductionLineConfig(@RequestBody ProjectProductionConfig projectProductionConfig) {
        logger.info("PUT /api/v1/productionLine/config updateProductionLineConfig.{}", JSONObject.toJSONString(projectProductionConfig));
        if( null == projectProductionConfig.getId()
                || null == projectProductionConfig.getProjectId()) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        return productionLineService.updateProductionLineConfig(projectProductionConfig);
    }

    @PostMapping(value = "/productionLine/config/validate", produces = "application/json;charset=UTF-8")
    public ResponseResult<Boolean> validateProductionLineConfig(@RequestBody ProjectProductionConfig projectProductionConfig) {
        logger.info("POST /api/v1/productionLine/config/validate validateProductionLineConfig.{}", JSONObject.toJSONString(projectProductionConfig));
        if(StringUtils.isEmpty(projectProductionConfig.getProductionTool())
                || StringUtils.isEmpty(projectProductionConfig.getProductionUrl())
                || StringUtils.isEmpty(projectProductionConfig.getProductionUsername())
                || StringUtils.isEmpty(projectProductionConfig.getProductionPassword())) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        return productionLineService.validateProductionLineConfig(projectProductionConfig);
    }

    @JwtIgnore
    @PostMapping(value = "/productionLine/notice/build/success", produces = "application/json;charset=UTF-8")
    public ResponseResult noticeBuildSuccess(@RequestBody @Valid ProductionLineBuildNoticeInfo productionLineBuildNoticeInfo, HttpServletRequest request) {
        logger.info("POST /api/v1/productionLine/notice/build/success noticeBuildSuccess.{}", JSONObject.toJSONString(productionLineBuildNoticeInfo));
        return productionLineService.noticeBuildSuccess(request, productionLineBuildNoticeInfo);
    }

    @PostMapping(value = "/productionLine/config/productproject", produces = "application/json;charset=UTF-8")
    public ResponseResult<Map<String, Map<String, String>>> getProductAndProject(@RequestBody ProjectProductionConfig projectProductionConfig) {
        logger.info("POST /productionLine/config/productproject getProductAndProject.{}", JSONObject.toJSONString(projectProductionConfig));
        if(StringUtils.isEmpty(projectProductionConfig.getBugUrl())
                || StringUtils.isEmpty(projectProductionConfig.getBugAccount())
                || StringUtils.isEmpty(projectProductionConfig.getBugPassword())
                || StringUtils.isEmpty(projectProductionConfig.getBugSubmitTo())) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        return productionLineService.getProductAndProject(projectProductionConfig);
    }

}
